package 代码记录.指定公司代码练习记录.西安.分值100分;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author zx
 * @create 2023-07-09 13:58
 * 人家的思路，其实就是力扣三数之和的思路(再看看)
 */
public class 双十一 {//80--应该可以做出来95%
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        int n = sc.nextInt();
        String[] temp = str.split(",");
        int[] arr = new int[temp.length];
        for(int i = 0;i < arr.length;i++){
            arr[i] = Integer.parseInt(temp[i]);
        }
        Arrays.sort(arr);
        int maxSum = -1;
        for (int i = 0; i < arr.length; i++) {
            int second = i + 1;
            int third = arr.length - 1;
            // 双指针在arr[i]后面的区间中寻找三数之和小于n的另外两个数(second和third)
            while (second < third) {
                int sum = arr[i] + arr[second] + arr[third];
                if (sum <= n) {
                    maxSum = Math.max(maxSum, sum);
                    second++;
                } else {
                    third--;
                }
            }
        }
        System.out.println(maxSum);
    }
}
